//
//  CCKSubscribeTableViewCell.m
//  budejieMe
//
//  Created by SuperJJ on 16/8/26.
//  Copyright © 2016年 SuperJJ. All rights reserved.
//

#import "CCKSubscribeTableViewCell.h"
#import "CCKSubscribeModel.h"



static const CGFloat kMargin = 8;

@interface CCKSubscribeTableViewCell ()

@property (nonatomic, weak) UIImageView *imgView;

@property (nonatomic, weak) UILabel *lblThemeText;

@property (nonatomic, weak) UILabel *lblDetailText;

@property (nonatomic, weak) UIButton *btnSub;

@end

@implementation CCKSubscribeTableViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"%s",__FUNCTION__);
    return [super initWithFrame:frame];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"%s",__FUNCTION__);
    return [super initWithCoder:aDecoder];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    __weak typeof(self) weakSelf = self;
    
    
    // imgView
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultTagIcon_110x110_@1x"]];
    [self.contentView addSubview:imgView];
    self.imgView = imgView;
    
    imgView.layer.cornerRadius = 5;
    imgView.clipsToBounds = YES;

    [imgView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.top.equalTo(weakSelf.contentView).offset(kMargin);
        make.bottom.equalTo(weakSelf.contentView).offset(-kMargin);
        make.width.equalTo(imgView.mas_height);
    }];
   
    
    // lblThemeText
    UILabel *lblThemeText = [[UILabel alloc] init];
    [self.contentView addSubview:lblThemeText];
    self.lblThemeText = lblThemeText;
    
    lblThemeText.text = @"标签";
    lblThemeText.textColor = [UIColor blackColor];
    lblThemeText.font = [UIFont systemFontOfSize:14];
    
    [lblThemeText mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(weakSelf.imgView.mas_right).offset(kMargin);
        make.top.equalTo(weakSelf.imgView);
    }];
    
    
    //lblDetailText
    UILabel *lblDetailText = [UILabel new];
    [self.contentView addSubview:lblDetailText];
    self.lblDetailText = lblDetailText;
    
    lblDetailText.text = @"100万人订阅 | 总贴数 12345678";
    lblDetailText.textColor = [UIColor grayColor];
    lblDetailText.font = [UIFont systemFontOfSize:12];
    
    [lblDetailText mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(imgView.mas_right).offset(kMargin);
        make.top.equalTo(lblThemeText.mas_bottom).offset(kMargin - 5);
    }];
    
    
    //btnSub
    UIButton *btnSub = [UIButton new];
    [self.contentView addSubview:btnSub];
    self.btnSub = btnSub;
    
    [btnSub setBackgroundImage:[UIImage imageNamed:@"FollowBtnBg"] forState:UIControlStateNormal];
    [btnSub setBackgroundImage:[UIImage imageNamed:@"FollowBtnClickBg"] forState:UIControlStateHighlighted];
    [btnSub sizeToFit];
    [btnSub setTitle:@"+ 订阅" forState:UIControlStateNormal];
    [btnSub setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btnSub.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [btnSub mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.centerY.equalTo(weakSelf.contentView).offset(0);
        make.right.equalTo(weakSelf.contentView).offset(- kMargin * 2);
    }];
    
    
    //分割线
    UIView *separator = [UIView new];
    [self.contentView addSubview:separator];
    separator.backgroundColor = [UIColor grayColor];
    separator.alpha = 0.4;
    [separator mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.offset(1);
    }];
    
}

-(void)setModel:(CCKSubscribeModel *)model
{
    _model = model;
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.image_list]];
    
    self.lblThemeText.text = model.theme_name;
    
    CGFloat subNum = [model.sub_number intValue] / 10000.0;
    
    NSString *strDetailText = [NSString stringWithFormat:@"%.1f万人订阅 | 总贴数 ", subNum];
    
    NSMutableAttributedString *attrDetailText = [[NSMutableAttributedString alloc] initWithString:strDetailText];
    
    NSAttributedString *attrPostNum = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", model.post_num] attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];
    
    [attrDetailText appendAttributedString:attrPostNum];
    
    [self.lblDetailText setAttributedText:attrDetailText];
}



@end
